package com.kaifamiao.servlet;

import com.kaifamiao.entity.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user/login")
public class UserLoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        final String url = request.getContextPath() + "/user.jsp";

        HttpSession session = request.getSession();

        String uname = request.getParameter("username");
        String passwd = request.getParameter("password");
        if("kaifamiao".equals(uname) && "kaifamiao".equals(passwd) ) {
            User u = new User();
            u.setUsername(uname);
            u.setPassword(passwd);
            // 将当前用户对象设置到会话属性中(也称作将当前用户绑定到会话中)
            session.setAttribute("user", u );
            response.sendRedirect(url);
            return;
        }

        session.setAttribute("message", "用户名或密码错误");
        response.sendRedirect(url);
    }

}
